package cn.bob.org.service.impl;

import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Dept;
import cn.bob.org.domain.User;
import cn.bob.org.mapper.UserMapper;
import cn.bob.org.query.UserQuery;
import cn.bob.org.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author bob
 * @data 2022/10/2-13:01
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void addUser(User user) {
        user.setCreatetime(new Date());
        user.setPassword("1");
        userMapper.addUser(user);
    }

    @Override
    @Transactional
    public void delByUser(Long id) {
        userMapper.delByUser(id);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        user.setCreatetime(new Date());
        userMapper.updateUser(user);
    }

    @Override
    public List<User> findUserAll() {
        return userMapper.findUserAll();
    }

    @Override
    public User findByUserId(Long id) {
        return userMapper.findByUserId(id);
    }

    @Override
    public Integer queryByUserCount(UserQuery query) {
        return userMapper.queryByUserCount(query);
    }

    @Override
    public PageList<User> queryByUserPage(UserQuery query) {
        if(!StringUtils.isEmpty(query.getKeyword())){
            query.setKeyword(query.getKeyword().trim());
        }
        Integer count = userMapper.queryByUserCount(query);
        List<User> users = userMapper.queryByUserPage(query);
        return  new PageList<User>(users,count);
    }

    @Override
    public User findUserLogin(User user) {
        User userLogin = userMapper.findUserLogin(user);
        System.out.println("1"+userLogin);
        if (userLogin !=null && userLogin.getStatus()==1){
            return userLogin;
        }
        return null;
    }

    @Override
    public void batchDel(List<Long> ids) {
        userMapper.batchDel(ids);
    }

    @Override
    public List<User> findByDeptUserId(Long id) {
        return userMapper.findByDeptUserId(id);
    }

}
